package com.carl.test;

import co.elastic.clients.elasticsearch.ElasticsearchAsyncClient;
import co.elastic.clients.elasticsearch.ElasticsearchClient;


import co.elastic.clients.elasticsearch.indices.*;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;


/**
 * @program: FrameStudy
 * @description:
 * @author: Mr.Carl
 **/
public class ESClient {

    public static ElasticsearchClient client;
    public static ElasticsearchAsyncClient asyncClient;

    public static ElasticsearchTransport transport;

    public static final String PERSON_INDEX = "person_index";

    public static  void creatIndexLambda()throws Exception{
        //获取索引客户端对象
        ElasticsearchIndicesClient indices = client.indices();

        //判断索引是否存在
        boolean flg = indices.exists(req->req.index(PERSON_INDEX)).value();
        if (flg){
            //索引存在

        }else {
            //创建索引 构建器方式构建
            indices.create(req->req.index(PERSON_INDEX));

            //XContentBuilder builder=XContentFactory.jsonBuilder();
        }
        //查询索引
        GetIndexResponse getIndexResponse = indices.get(get->get.index(PERSON_INDEX));

        //删除索引
        DeleteIndexResponse deleteIndexResponse = indices.delete(del->del.index(PERSON_INDEX));

        transport.close();
    }

    private static void creatIndex() throws Exception {
        //获取索引客户端对象
        ElasticsearchIndicesClient indices = client.indices();


        //判断索引是否存在
        ExistsRequest existsRequest = new ExistsRequest.Builder().index(PERSON_INDEX).build();
        boolean flg = indices.exists(existsRequest).value();






        if (flg){
            //索引存在

        }else {
            //创建索引 构建器方式构建
            CreateIndexRequest request = new CreateIndexRequest.Builder().index(PERSON_INDEX).build();

            indices.create(request);
        }
        //查询索引
        GetIndexRequest getIndexRequest = new GetIndexRequest.Builder().index(PERSON_INDEX).build();
        GetIndexResponse getIndexResponse = indices.get(getIndexRequest);

        //删除索引
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest.Builder().index(PERSON_INDEX).build();
        DeleteIndexResponse deleteIndexResponse = indices.delete(deleteIndexRequest);

        transport.close();
    }

    public static void init() throws Exception {
        // 创建 RestClient
        RestClientBuilder builder = RestClient.builder(new HttpHost("101.43.4.193", 9200, "http"));
        RestClient restClient = builder.build();

        transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());
        client = new ElasticsearchClient(transport);
        asyncClient = new ElasticsearchAsyncClient(transport);
        //transport.close();
    }

    public void creatIndexTest(){

    }

}
